<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>布局-综合场景</title>
</head>

<body>
  <button id="change_layout">栅格布局</button>
  <button id="random_layout">打散布局</button>
  <button id="layout">整理布局</button>
  <div id="mountNode"></div>
  <script src="../build/g6.js"></script>
  <script src="../build/utilRandomData.js"></script>
  <script src="../build/behaviourAnalysis.js"></script>
  <script>
    const Util = G6.Util;
    const data = Util.createChainData(50);
    const width = window.innerWidth;
    const height = window.innerHeight;
    const num = 50;
    let setCol = 5;
    let node;


    // 布局生成器
    const layoutCreator = col => {
      return nodes => {
        const hgap = 76;
        const vgap = 50;

        nodes.forEach((node, index) => {
          if (parseInt(index / col) % 2 === 0) {
            node.x = (col - index % col) * hgap;
          } else {
            node.x = index % col * hgap + hgap;
          }
          node.y = parseInt(index / col) * vgap + vgap / 2;
        });
      }
    };
    const graph = new G6.Graph({
      container: 'mountNode',
      width,
      height,
      animate: true,
      layout: {
        processer: layoutCreator(setCol),
        auto: false
      },
      modes: {
        default: ['panNode']
      },
    });
    graph.read(data);
    graph.layout();
    document.getElementById('layout').onclick = () => {
      graph.layout();
    };
    document.getElementById('change_layout').onclick = () => {
      if (setCol === 5) {
        setCol = 6;
      } else {
        setCol = 5;
      }
      graph.changeLayout(layoutCreator(setCol));
    };
    document.getElementById('random_layout').onclick = () => {
      graph.changeLayout(nodes => {
        nodes.forEach(node => {
          node.x = width * Math.random();
          node.y = height * Math.random();
        })
      });
    };
  </script>
</body>
</html>
